package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DimensionInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchDimensionInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IDimensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 维度管理
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/dimension")
public class DimensionController extends BaseController {
    @Autowired
    IDimensionService service;
    @Autowired
    private RedisTemplate redisTemplate;
    private String PREFIX = "/basicData/dimension/";

    /**
     * 跳转到维度管理页面
     */
    @RequestMapping("")
    public String dimension(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "dimension.html";
    }

    /**
     * 跳转到维度管理添加页面
     */
    @RequestMapping(value = "/dimension_add")
    public String dimensionAdd() {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "dimension_add.html";
    }

    /**
     * 跳转到维度管理详情页面
     */
    @RequestMapping(value = "/dimension_update/{dimensionId}")
    public String dimensionUpdate(@PathVariable String dimensionId, Model model) {
        if (ToolUtil.isEmpty(dimensionId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        DimensionInfo dimensionInfo = this.service.selectOneById(dimensionId);
        model.addAttribute("dimensionInfo", dimensionInfo);
        return PREFIX + "dimension_edit.html";
    }

    /**
     * 维度管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchDimensionInfo info, HttpServletRequest request) {
        Page<DimensionInfo> page = new PageFactory<DimensionInfo>().defaultPage();
        try {
            List<DimensionInfo> list = service.selectPageListByQueryGetMap(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 添加维度
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid DimensionInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }

    /**
     * 删除维度
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        service.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改维度
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid DimensionInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getList() {
        if (ShiroKit.getSessionAttr("dimensionList") == null) {
            ShiroKit.setSessionAttr("dimensionList", service.list());
            return ShiroKit.getSessionAttr("dimensionList");
        } else {
            return ShiroKit.getSessionAttr("dimensionList");
        }
    }
}